package com.cbgolf.oa.views;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.AutoTextViewAdapter;
import com.cbgolf.oa.base.BaseView;
import com.cbgolf.oa.entity.ParkBean;
import com.cbgolf.oa.presenter.ILoginPresenter;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.ScreenUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by lq on 2017/9/13.
 */

public class LoginView extends BaseView implements AutoTextViewAdapter.IAutoTextViewWorker {

  @BindView(R.id.login_account_et)
  EditText accountEt;
  @BindView(R.id.login_pwd_et)
  EditText pwdEt;
  @BindView(R.id.login_submit_tv)
  TextView subMitTv;
  @BindView(R.id.login_park_atv)
  AutoCompleteTextView parkAtv;
  /**
   * var
   */
  private String account;
  private String pwd;

  private ILoginPresenter worker;


  public LoginView(Activity act, ILoginPresenter worker) {
    this.worker = worker;
    ButterKnife.bind(this, act);
    super.init(act);
  }

  @Override
  protected void setViews() {
    ViewUtils.setText(parkAtv, DataUtil.getLoginPark());
    ViewUtils.setText(accountEt, DataUtil.getLoginAccountChild());
    startName = Util.isNull(DataUtil.getLoginPark()) ? "" : DataUtil.getLoginAccountStart();
  }

  @Override
  protected void setListener() {
    subMitTv.setOnClickListener(v -> {
      account = accountEt.getText().toString().trim();
      pwd = pwdEt.getText().toString().trim();
      String name = parkAtv.getText().toString();
      if (Util.isNull(startName) && !Util.listIsNull(listParks)) {
        for (int i = 0; i < listParks.size(); i++) {
          if (Util.isEquals(listParks.get(i).courseName, name)) {
            startName = listParks.get(i).userAccount;
            break;
          }
        }
      }
      worker.subMit(account, pwd, name, startName);
    });
    parkAtv.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {

      }

      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {

      }

      @Override
      public void afterTextChanged(Editable s) {
        if (null != adapter) {
          adapter.clear();
          adapter.notifyDataSetChanged();
        }
        String name = parkAtv.getText().toString();
        if (Util.isNull(name)) {
          startName = "";
          return;
        }
        if (Util.isEquals(checkName, name)) {
          return;
        }
        startName = "";
        worker.searchPark(name);
      }
    });
    parkAtv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        parkAtv.setText((!Util.listIsNull(listParks) && listParks.size() > position) ?
            (TextUtil.Text(listParks.get(position).courseName))
            : "");
      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {

      }
    });

  }


  private String startName;
  private String checkName;
  private List<ParkBean> listParks = new ArrayList<>();

  public void getParkSuccess(List<ParkBean> list, String name) {
    listParks.clear();
    listParks.addAll(list);
    String parkName = parkAtv.getText().toString().trim();
    if (Util.listIsNull(list) || !Util.isEquals(parkName, name)) {
      startName = null;
      return;
    }
    setAutoTextView(list, name);
  }

  private AutoTextViewAdapter adapter;

  private void setAutoTextView(final List<ParkBean> list, String name) {
    adapter = new AutoTextViewAdapter(context, list, name, this);
    parkAtv.setAdapter(adapter);
    parkAtv.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    if (list.size() > 8) {
      parkAtv.setDropDownHeight(ScreenUtil.getHeight() / 2);
      parkAtv.setMaxLines(10);
    } else {
      parkAtv.setDropDownHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    }
    parkAtv.showDropDown();
  }

  @Override
  public void onClick(String name, String id) {
    parkAtv.dismissDropDown();
    checkName = name;
    startName = id;
    parkAtv.setText(TextUtil.Text(name));
  }
}

